//
//  IDInformationConfirmController.swift
//  EasyDana
//
//  Created by mac on 2024/10/14.
//

import UIKit
import RxSwift
import RxCocoa
class IDInformationConfirmController: UIViewController {

    var product_id = ""
    
    var cardModels: [UploadIDCardModel.CardInfoModel] = []
    
    private var models: [BehaviorRelay<UploadIDCardModel.CardInfoModel>] = []
    
    let bag = DisposeBag()
    
//    private let done
    let doneNotifier = PublishSubject<Bool>()
    
    @IBOutlet weak var contentBGView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.configView()
        // Do any additional setup after loading the view.
    }
    
    
    @IBAction func closeAction(_ sender: UIButton) {
        self.dismiss(animated: true)
    }
    
    private func configView() {
        self.models = self.cardModels.map({ model in
            return BehaviorRelay(value: model)
        })//openly    String    "chunam"
        for (index, model) in self.models.enumerated() {
            let view = UIView()
            view.backgroundColor = UIColor.clear
            self.view.addSubview(view)
            view.snp.makeConstraints { make in
                make.width.equalTo(270)
                make.height.equalTo(55)
                make.centerX.equalTo(self.contentBGView)
                make.top.equalTo(self.contentBGView).offset(15 + 70 * index)
            }
            let lbl = UILabel()
            view.addSubview(lbl)
            lbl.textColor = UIColor.color(with: 102, green: 102, blue: 102)
            lbl.font = UIFont.systemFont(ofSize: 14)
            lbl.snp.makeConstraints { make in
                make.left.equalToSuperview()
                make.top.equalToSuperview()
            }
            model.map { mod in
                return mod.frost
            }
            .bind(to: lbl.rx.text)
            .disposed(by: self.bag)
            let field = UITextField()
            view.addSubview(field)
            field.font = UIFont.systemFont(ofSize: 16)
            field.textColor = UIColor.black
            field.snp.makeConstraints { make in
                make.left.right.equalToSuperview()
                make.height.equalTo(20)
                make.top.equalTo(lbl.snp.bottom).offset(8)
            }
            model.map { mod in
                return mod.scarred
            }.bind(to: field.rx.text)
                .disposed(by: self.bag)
            field.rx.text.orEmpty.withLatestFrom(model) { text, current -> UploadIDCardModel.CardInfoModel in
                var nmodel = current
                nmodel.scarred = text
                return nmodel
            }
            .bind(to: model)
            .disposed(by: self.bag)
            if model.value.openly == "chunam" {
                let button = UIButton(type: .system)
                view.addSubview(button)
                button.snp.makeConstraints { make in
                    make.edges.equalToSuperview()
                }
                button.rx.tap
                    .subscribe(onNext: { event in
                        let birthdayVC = DatePickerViewController()
                        birthdayVC.confirm
                            .bind(to: field.rx.text)
                            .disposed(by: self.bag)
                        self.alert(viewController: birthdayVC)
                    })
                    .disposed(by: self.bag)
            }
            let separatorView = UIView()
            view.addSubview(separatorView)
            separatorView.backgroundColor = UIColor.color(with: 7, green: 7, blue: 7, alpha: 0.05)
            separatorView.snp.makeConstraints { make in
                make.left.right.equalToSuperview()
                make.height.equalTo(1)
                make.bottom.equalToSuperview()
            }
        }
    }

    @IBAction func submitAction(_ sender: UIButton) {
        Task {
            do {
                self.showLoading(text: nil)
                var param: [String: String] = ["guns": self.product_id]
                for model in self.models {
                    param[model.value.openly] = model.value.scarred
                }
                let res = try await HTTPManager.default.post(path: Api.saveUserIDCard, param: param, dataType: [Int].self)
                if res.code == 0 {
                    self.dismiss(animated: false) {
                        self.doneNotifier.onNext(true)
                    }
                } else {
                    self.hideLoading()
                    self.show(toast: res.state)
                }
            } catch {
                self.hideLoading()
            }
        }
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
